\section{Ap\'endice B}
\subsection{C\'odigo}
\subsubsection{El main}
\begin{verbatim}
#include <iostream>
#include <string>
#include <fstream>
#include "Float.h"
using namespace std;
#define APROXRAIZ2 1.4
#define PRECISION 52
#define TERMINOS 170
#define SALIDA "resultados.out"
#define GRAFICOS 0

Float metodoBabilonio(Float aproximacion,int precision,int terminos);
Float metodoBinomial(int precision,int terminos);


int main(int argc, char* argv[])
{
  int precision=PRECISION;
  Float aprox(precision,APROXRAIZ2);
  int terminos=TERMINOS;
  string salida = SALIDA;
  string s;
  bool graficar = GRAFICOS;
  //toma los argumentos pasados como parametro
  for(int i=0;i<argc;i++)
  {
    s = argv[i]; 
    if(s == "-p")
      precision = atoi(argv[i+1]);
    else
      if(s == "-a")
        aprox = Float(precision,atof(argv[i+1]));
      else
        if(s == "-t")
          terminos = atoi(argv[i+1]);
        else
          if(s == "-o")
            salida = argv[i+1];
          else
            if(s == "-g")
              graficar = true;
  }
  
  if(graficar)
  {
    long double raizDOUBLE = sqrt(2);
    Float raizFLOAT(PRECISION,sqrt(2));
    ofstream fsfba,fsdba,fsfbin,fsdbin;
  
    //aca vario la precision para 10 terminos
    fsfba.open("varPrec10BabilonioFLOAT.txt");
    fsdba.open("varPrec10BabilonioDOUBLE.txt");
    fsfbin.open("varPrec10BinomialFLOAT.txt");
    fsdbin.open("varPrec10BinomialDOUBLE.txt");
    for(int p=1;p<=PRECISION;p++)
    {
      Float resBab = metodoBabilonio(aprox,p,10);
      Float resBin = metodoBinomial(p,10);
      
      fsfba<<p<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<p<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
      
      fsfbin<<p<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<p<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
      
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la precision para 50 terminos
    fsfba.open("varPrec50BabilonioFLOAT.txt");
    fsdba.open("varPrec50BabilonioDOUBLE.txt");
    fsfbin.open("varPrec50BinomialFLOAT.txt");
    fsdbin.open("varPrec50BinomialDOUBLE.txt");
    for(int p=1;p<=PRECISION;p++)
    {
      Float resBab = metodoBabilonio(aprox,p,50);
      Float resBin = metodoBinomial(p,50);
      
      fsfba<<p<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<p<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
          
      fsfbin<<p<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<p<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
      
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la precision para 100 terminos
    fsfba.open("varPrec100BabilonioFLOAT.txt");
    fsdba.open("varPrec100BabilonioDOUBLE.txt");
    fsfbin.open("varPrec100BinomialFLOAT.txt");
    fsdbin.open("varPrec100BinomialDOUBLE.txt");
    for(int p=1;p<=PRECISION;p++)
    {
      Float resBab = metodoBabilonio(aprox,p,100);
      Float resBin = metodoBinomial(p,100);
          
      fsfba<<p<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<p<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
          
      fsfbin<<p<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<p<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
      
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la precision para 170 terminos
    fsfba.open("varPrec170BabilonioFLOAT.txt");
    fsdba.open("varPrec170BabilonioDOUBLE.txt");
    fsfbin.open("varPrec170BinomialFLOAT.txt");
    fsdbin.open("varPrec170BinomialDOUBLE.txt");
    for(int p=1;p<=PRECISION;p++)
    {
      Float resBab = metodoBabilonio(aprox,p,170);
      Float resBin = metodoBinomial(p,170);
          
      fsfba<<p<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<p<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
          
      fsfbin<<p<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<p<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
          
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
        
    //aca vario la cantidad de terminos para 10 bits de precision
    fsfba.open("varTer10BabilonioFLOAT.txt");
    fsdba.open("varTer10BabilonioDOUBLE.txt");
    fsfbin.open("varTer10BinomialFLOAT.txt");
    fsdbin.open("varTer10BinomialDOUBLE.txt");
    for(int t=1;t<=TERMINOS;t++)
    {
      Float resBab = metodoBabilonio(aprox,10,t);
      Float resBin = metodoBinomial(10,t);
              
      fsfba<<t<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<t<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
              
      fsfbin<<t<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<t<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
              
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la cantidad de terminos para 25 bits de precision
    fsfba.open("varTer25BabilonioFLOAT.txt");
    fsdba.open("varTer25BabilonioDOUBLE.txt");
    fsfbin.open("varTer25BinomialFLOAT.txt");
    fsdbin.open("varTer25BinomialDOUBLE.txt");
    for(int t=1;t<=TERMINOS;t++)
    {
      Float resBab = metodoBabilonio(aprox,25,t);
      Float resBin = metodoBinomial(25,t);
                  
      fsfba<<t<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<t<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
                  
      fsfbin<<t<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<t<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
                  
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la cantidad de terminos para 40 bits de precision
    fsfba.open("varTer40BabilonioFLOAT.txt");
    fsdba.open("varTer40BabilonioDOUBLE.txt");
    fsfbin.open("varTer40BinomialFLOAT.txt");
    fsdbin.open("varTer40BinomialDOUBLE.txt");
    for(int t=1;t<=TERMINOS;t++)
    {
      Float resBab = metodoBabilonio(aprox,40,t);
      Float resBin = metodoBinomial(40,t);
                  
      fsfba<<t<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<t<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
                  
      fsfbin<<t<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<t<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
                  
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
    
    //aca vario la cantidad de terminos para 52 bits de precision
    fsfba.open("varTer52BabilonioFLOAT.txt");
    fsdba.open("varTer52BabilonioDOUBLE.txt");
    fsfbin.open("varTer52BinomialFLOAT.txt");
    fsdbin.open("varTer52BinomialDOUBLE.txt");
    for(int t=1;t<=TERMINOS;t++)
    {
      Float resBab = metodoBabilonio(aprox,52,t);
      Float resBin = metodoBinomial(52,t);
                  
      fsfba<<t<<"\t\t"<<(raizFLOAT - resBab).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdba<<t<<"\t\t"<<fabs((raizDOUBLE - resBab.getValue()))/raizDOUBLE<<endl;
                  
      fsfbin<<t<<"\t\t"<<(raizFLOAT - resBin).getAbsValue()/raizFLOAT.getAbsValue()<<endl;
      fsdbin<<t<<"\t\t"<<fabs((raizDOUBLE - resBin.getValue()))/raizDOUBLE<<endl;
                  
    }
    fsfba.close();
    fsdba.close();
    fsfbin.close();
    fsdbin.close();
  }  
  return 0;
}

Float metodoBabilonio(Float aproximacion,int precision,int terminos)
{
  Float res = aproximacion;
  Float aux(precision,2.00);
  for(int i=0;i<terminos;i++)
  {
    res = (res + (aux/res))/aux;
  }
  
  return res;
}

Float metodoBinomial(int precision,int terminos)
{
  Float res(precision,1.00);
  Float anterior(precision, 0);
  Float uno(precision,1);
  Float fact(precision,1);
  Float ene(precision,0.5);
  Float acum = ene;
  Float it(precision,1);
  
  while (it.getValue() < terminos && res != anterior){
    anterior = res;
    res = res + acum/fact;
    acum = acum*(ene - it);
    it= it + uno;
    fact = fact * it;  
  }
  
  return res;
}
\end{verbatim}
\subsubsection{Implementaci\'on n\'umero de punto flotante}
\begin{verbatim}
#ifndef FLOAT_H_
#define FLOAT_H_

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

class Float {
  public:
    Float(int precision, long double value);
    ~Float();
    const int getExp() const;
    const string getMant() const;
    const int getPrecision() const;
    const long double getValue() const;
    const long double getAbsValue() const;
    const string getSigno() const;
    Float operator+ (Float aFloat);
    Float operator- (Float aFloat);
    Float operator/ (Float aFloat);
    Float operator* (Float aFloat);
    Float & operator= (const Float &);
    
    friend int operator== (const Float&, const Float&);
    friend int operator!= (const Float&, const Float&);
    friend ostream &operator<<(ostream &stream, Float &ob);
  private:
    void longDoubleToBinary(long double);
    long double binaryToLongDouble();
    //signo si es negativo el numero
    bool _neg;
    //representa el valor al cual esta elevado el 2, o sea 2^exp.
    int _exp;
    //cant. de bits que se usan en la mantisa.
    int _precision;
    //representacion en binario de la mantisa.
    string _mant;
    //valor con el que se trabaja.
    long double _value;
};

#endif /*FLOAT_H_*/


#include "Float.h"

Float::Float(int precision, long double value) {
  _precision = precision;
  _mant = "";
  longDoubleToBinary(value);
  _value = binaryToLongDouble();
}

Float::~Float() {
}

/**
* Calcula la mantisa y el exponente del numero que figura en value.
*/
void Float::longDoubleToBinary(long double value) {
  int sizeMantisa = _precision;
  long double x, mant;
  x = value;
  if (x < 0) { 
    x = ((-1) * value);
  } 
  _neg = (value < 0);
  int exp = 0;
  // Ahora se busca la mantisa
  mant = x;
  while (mant >= 1) {
    mant /= 2;
          exp++;
        // en este punto x = mant * 2^(exp)
  }

  while (mant < 0.5 && mant != 0) {
    mant *= 2;
          exp--;
  }

  _exp = exp;
  while (mant > 0 && sizeMantisa > 0) {
    mant *= 2;
          if (mant >= 1) {
      _mant += "1";
      mant -= 1;
          } else {
      _mant += "0";
    }
    sizeMantisa--;
      }
  while (sizeMantisa > 0) {
    _mant += "0";
    sizeMantisa--;
  }
}

Float Float::operator+(Float ob1) {
  long double result = _value + ob1.getValue();
  return Float(_precision, result);
}

Float Float::operator-(Float obj) {
  long double result = _value - obj.getValue();
  return Float(_precision, result);
}

Float Float::operator/(Float obj) {
  long double result = _value / obj.getValue();
  return Float(_precision, result);
}

Float& Float::operator=(const Float& obj) {
  if (this == &obj) 
    return *this;
  _precision = obj.getPrecision();
  _value = obj.getValue();
  _exp = obj.getExp();
  _mant = obj.getMant();
  return *this;
}

Float Float::operator*(Float obj) {
  long double result = _value * obj.getValue();
  return Float(_precision, result);
}

int operator== (const Float& a, const Float& b){
  if (a.getValue()==b.getValue())
    return 1;
  else
    return 0;
}

int operator!= (const Float& a, const Float& b){
  if (a.getValue() != b.getValue())
    return 1;
  else
    return 0;
}

const int Float::getPrecision() const {
  return _precision;
}

const long double Float::getValue() const {
  return _value;
}

const long double Float::getAbsValue() const {
  if (_value < 0)
    return 0 - _value;
  else
    return _value;
}

const int Float::getExp() const {
  return _exp;
}

const string Float::getMant() const{
  return _mant;
}

long double Float::binaryToLongDouble() {
  long double result, suma = 0;
  result = pow((double)2,(double)_exp);
  string bit;
  for(int i=0; i<_mant.size(); i++) {
    bit = _mant[i];
    suma += (atoi(bit.c_str()) * pow((double)2,(double)(-i-1)));
  }
  if (_neg) {
    return (-1)*(result * suma);
  }
  return (result * suma);
  
}

string const Float::getSigno() const {
  return (_neg ? "negativo" : "positivo");
}

ostream &operator<<(ostream &stream, Float &ob)
{
  stream << "signo: " << ob.getSigno() << endl;
  stream.precision(20);
  stream << "valor decimal: " << ob.getValue() << endl;
  //para graficos en excel
  //stream << ob.getValue();
  stream << "exp: " << ob.getExp() << endl;
  stream << "mantisa : "  << ob.getMant() << endl;
  return stream;
}

\end{verbatim}
\newpage